package com.wsc.wisdomchinaqinghe.activitity.minemodule;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.adapter.WodeBaoming_Adapter;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.LouYuHuoDongListBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;

import java.util.List;

/**
 * 
 * @Description 新股列表页
 * @author jaily.zhang
 * @date 2015-12-28 下午2:08:43
 * @version V1.3.1
 */
public class MyBaoMingActivity extends BaseActivity implements OnClickListener {

	private PullToRefreshListView lv_today_information;
	private ListView actualListView;
	private Context mcontext;
	private int skip = 0;
	private final int limit = 6;
	private ContributorPresenter contributorPresenter;
	private final String HOME_NEWS = MyBaoMingActivity.class.getSimpleName();
	private List<LouYuHuoDongListBean> homeNewsList;
	private WodeBaoming_Adapter quanBuAdapter;
	private boolean isfresh = true;
	private ProgressBar pb_todayInfo;
	private ImageView regist_back;
	private TextView tv_headtitle;
	private RelativeLayout layout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mybaoming_layout);
		mcontext = this;
		contributorPresenter = new ContributorPresenter(this, mcontext);
		initView();
		showData();
	}

	/**
	 * @Description 初始化View控件
	 * @author jaily.zhang
	 */
	private void initView() {
		regist_back=(ImageView) findViewById(R.id.regist_back);
		regist_back.setOnClickListener(this);
		tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
		tv_headtitle.setText("活动报名");
		lv_today_information = (PullToRefreshListView)findViewById(R.id.lv_today_information);
		lv_today_information.setMode(PullToRefreshBase.Mode.BOTH);
		pb_todayInfo = (ProgressBar)findViewById(R.id.pb_todayInfo);
		actualListView = lv_today_information.getRefreshableView();
		layout=(RelativeLayout) findViewById(R.id.layout);
		actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
		Tools.showRefreshView(lv_today_information);

		lv_today_information.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				// Update the LastUpdatedLabe
				// 处理下拉刷新的业务
				if (refreshView.getCurrentMode().equals(PullToRefreshBase.Mode.PULL_FROM_START)) {
					isfresh = true;
					Tools.showRefreshView(lv_today_information);
					skip = 0;
					showData();
				} else {
					isfresh = false;
					Tools.showRefreshView(lv_today_information);
					skip += limit;
					showData();
				}

			}
		});

		actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
				Intent intent = new Intent(mcontext, BaoMingDetailActivity.class);
				intent.putExtra("id", homeNewsList.get(position-1).getId());
				startActivityForResult(intent,0);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==0 && resultCode==1)
		{
			isfresh=true;
			skip=0;
			showData();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId())
		{
			case R.id.regist_back:
				finish();
				break;
		}

	}

	/**
	 * @Description 服务器获取数据
	 * @author jaily.zhang
	 */
	private void showData() {
		String uid=Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",mcontext);
		//首页资讯
		contributorPresenter.getActivitySignup(uid,skip, limit, HOME_NEWS);
	}

	@Override
	public void onLoadContributorStart(String flag) {

	}


	@Override
	public void onLoadContributorSuccess(String jsonString, String flag) {
		try {
			if (flag.equals(HOME_NEWS)) {
				RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
				String code = respObj.getCode();
				Object object = respObj.getMessage();
				if (code.equals("200") && object != null) {
					String jsonMessage = JsonTools.objectToJsonString(object);
					List<LouYuHuoDongListBean> homeNewsItem = JsonTools.jsonStringToList(jsonMessage, LouYuHuoDongListBean.class);
					if (homeNewsItem != null && homeNewsItem.size() > 0) {
						actualListView.setVisibility(View.VISIBLE);
						if (isfresh) {
							layout.setVisibility(View.GONE);
							homeNewsList = homeNewsItem;
							quanBuAdapter = new WodeBaoming_Adapter(mcontext, homeNewsList);
							actualListView.setAdapter(quanBuAdapter);
						} else {
							layout.setVisibility(View.GONE);
							homeNewsList.addAll(homeNewsItem);
							quanBuAdapter.notifyDataSetChanged();
						}

					}else {
						if(isfresh)
						{
							layout.setVisibility(View.VISIBLE);
							actualListView.setVisibility(View.GONE);
						}
					}

				}
			}
		} catch (Exception e) {

		}
	}


	@Override
	public void onLoadContributorComplete(String flag) {
		lv_today_information.onRefreshComplete();
		pb_todayInfo.setVisibility(View.GONE);
	}

	@Override
	public void onLoadContributorError(String error, String flag) {
	}

}
